package com.example.datacollection.service.impl;

import com.example.datacollection.api.PopulationCommonMethod;
import com.example.datacollection.bean.DataDemography;
import com.example.datacollection.constants.PopulationConstants;
import com.example.datacollection.mapper.DataDemographyMapper;
import lombok.extern.java.Log;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Log
@Component
public class PopulationCollection extends SumCollection {
    @Value("${collection.url}")
    private String url;
    @Autowired
    DataDemographyMapper dataDemographyMapper;

    @Override
    public String collection() {
        // ChromeOptions：创建浏览器启动属性类
        ChromeOptions chromeOptions = new ChromeOptions();
        // 设置后台静默模式启动浏览器
        chromeOptions.addArguments("--headless");
        //创建驱动
        WebDriver driver = new ChromeDriver(chromeOptions);
        //设置浏览器窗口大小
        driver.manage().window().maximize();
        //设置加载等待时长
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        driver.get(url);
        WebElement elementNum = driver.findElement(By.xpath("//*[@id=\"treeZhiBiao_4_a\"]"));
        elementNum.click();
        elementNum.click();
        JavascriptExecutor executors = (JavascriptExecutor) driver;
        executors.executeScript("document.getElementById('treeZhiBiao_4_ul').style.display='block';");
        WebElement elementSumNum = driver.findElement(By.xpath("//*[@id=\"treeZhiBiao_30_a\"]"));
        elementSumNum.click();
        List<WebElement> elementOnes = driver.findElements(By.xpath("//*[@id=\"table_main\"]/tbody/tr"));
        int numOne = 1;
        Map<Integer, List<String>> map = new HashMap();
        map.put(1, new ArrayList<>());
        map.put(2, new ArrayList<>());
        map.put(3, new ArrayList<>());
        map.put(4, new ArrayList<>());
        map.put(5, new ArrayList<>());
        map.put(6, new ArrayList<>());

        for (WebElement elementOne : elementOnes) {
            List<WebElement> elementTwos = elementOne.findElements(By.xpath(".//td"));
            elementTwos.remove(0);
            for (WebElement elementTwo : elementTwos) {
                map.get(numOne).add(elementTwo.getText());
            }
            numOne++;
        }
        List<WebElement> elementTwos = driver.findElements(By.xpath("//*[@id=\"main-container\"]/div[2]/div[2]/div[2]/div/div[2]/table/thead/tr/th"));
        elementTwos.remove(0);
        ArrayList<Object> listTwo = new ArrayList<>();
        PopulationCommonMethod.collection(elementTwos, listTwo, driver);
        driver.close();
        int objectLength = map.get(1).size();
        List<DataDemography> listDemography = new ArrayList<>();
        for (int i = 1; i <= 5; i++) {
            for (int j = 0; j < objectLength; j++) {
                if (i == 1) {
                    DataDemography dataDemography = new DataDemography();
                    switch (i) {
                        case 1:
                            dataDemography.setPopulationSum(Integer.parseInt(map.get(i).get(j)));
                            break;
                        case 2:
                            dataDemography.setPopulationMale(Integer.parseInt(map.get(i).get(j)));
                            break;
                        case 3:
                            dataDemography.setPopulationFemale(Integer.parseInt(map.get(i).get(j)));
                            break;
                        case 4:
                            dataDemography.setPopulationTown(Integer.parseInt(map.get(i).get(j)));
                            break;
                        case 5:
                            dataDemography.setPopulationRural(Integer.parseInt(map.get(i).get(j)));
                            break;
                    }
                    dataDemography.setStatisticalYears(listTwo.get(j).toString());
                    listDemography.add(dataDemography);
                } else {
                    switch (i) {
                        case 1:
                            listDemography.get(j).setPopulationSum(Integer.parseInt(map.get(i).get(j)));
                            break;
                        case 2:
                            listDemography.get(j).setPopulationMale(Integer.parseInt(map.get(i).get(j)));
                            break;
                        case 3:
                            listDemography.get(j).setPopulationFemale(Integer.parseInt(map.get(i).get(j)));
                            break;
                        case 4:
                            listDemography.get(j).setPopulationTown(Integer.parseInt(map.get(i).get(j)));
                            break;
                        case 5:
                            listDemography.get(j).setPopulationRural(Integer.parseInt(map.get(i).get(j)));
                            break;
                    }
                    listDemography.get(j).setStatisticalYears(listTwo.get(j).toString());
                }
            }
        }
        dataDemographyMapper.deletePopulation();
        dataDemographyMapper.savePopulation(listDemography);
        log.info("-----结束----");
        return PopulationConstants.COLLECTION_SUCCESSFUL;
    }
}
